package galaxies.starRegions;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import com.jme3.math.Vector2f;

public class StarRegionEllipticalBuilder {

	/*
	 * Creates a series of elliptical arc areas
	 */
	public ArrayList<StarRegionModel> buildRegions(ArrayList<ArrayList<Integer>> dims) {
		
		//find largest and smallest x, y values in dims
		double dist;
		double maxDist = 0, minDist = 10000;
		
		//assume creation of galaxy around (0,0) point
		for (ArrayList<Integer> point: dims) {		
			dist = Math.sqrt(point.get(0)*point.get(0) + point.get(1)*point.get(1));
			
			if (dist > maxDist) {
				maxDist = dist;
			}
			else if (dist < minDist) {
				minDist = dist;
			}
		}
		
		/*
		 * Can now define the short and long dimensions of the ellipse.
		 * 
		 * Always have a minimum of 4 sectors in any galaxy.  Additional sectors created if these 4 sectors
		 * would be too large.
		 * 
		 * Max sector radial length = 40
		 */
		double xDim = maxDist;//(maxX - minX)/2;
		double yDim = minDist;//(maxY - minY)/2;
		
//		System.out.println("xDim= " + xDim);
//		System.out.println("yDim= " + yDim);
		
		double maxRegionRadius = 40;//maxDist/4;//40;
		
		int numSectorsX = (int) Math.ceil(xDim/maxRegionRadius);
		int numSectorsY = (int) Math.ceil(yDim/maxRegionRadius);
		
		//Must have even number of sectors in both x and y directions
		if (numSectorsX > numSectorsY) {
			numSectorsY = numSectorsX;
		}
		else {
			numSectorsX = numSectorsY;
		}
		
		//maintain at least 4 sectors total
		if (numSectorsX < 2)
			numSectorsX = 2;
		
		if (numSectorsY < 2)
			numSectorsY = 2;
		
		float xRegionSide = (float) xDim/(float) numSectorsX;
		float yRegionSide = (float) yDim/(float) numSectorsY;
		
		ArrayList<StarRegionModel> regions = new ArrayList<StarRegionModel>();
		
		double theta = 0;
		double thetaStep = Math.PI/4;
		
		float startX = 0;
		float startY = 0;
		double x, y;
		double resolution = 50;
		double phiStep = thetaStep/resolution;
		//step through angles
		//ArrayList<Vector2f> hashPoints = new ArrayList<Vector2f>(); 
		ArrayList<Vector2f> points = new ArrayList<Vector2f>();
		
		for (theta = 0; theta < 2*Math.PI; theta+=thetaStep) {
			//step through arc segments in the current angular focus
			System.out.println("Theta= " + theta);
			for (int i = 0; i < numSectorsX; i++) {
				System.out.println("Sector#= " + i);
				if ((startX == 0) || (startY == 0)) {
					Vector2f temp = new Vector2f(0,0);
					points.add(temp);
				}
				else {
					//sweep out arc from theta to theta+thetaStep
					for (double phi = theta; phi <= theta+thetaStep; phi+=phiStep) {
						x = startX*Math.cos(phi);
						y = startY*Math.sin(phi);
						
						Vector2f temp = new Vector2f((float)x,(float)y);
						points.add(temp);						
					}
				}
				
				startX+=xRegionSide;
				startY+=yRegionSide;
				
				//sweep out the other side of the area, in reverse phi direction
				for (double phi = theta+thetaStep; phi >= theta; phi-=phiStep) {
					x = startX*Math.cos(phi);
					y = startY*Math.sin(phi);
					
					Vector2f temp = new Vector2f((float)x,(float)y);
					points.add(temp);						
				}				
				
				regions.add(new StarRegionModel(points));				
				points.clear();
			}
			startX = 0;
			startY = 0;
		}
		
		return regions;
	}
	
	
}
